package hqu.jann.controller;

import hqu.jann.pojo.Course;
import hqu.jann.dao.CourseList;
import hqu.jann.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;



@Controller
public class HelloController {
    CourseList courseList = new CourseList();
    CourseList selectList = new CourseList();
    Student student1 = new Student(1925122037, "jann", "123456", "男", 2019, "软件工程", "计算机科学与技术学院");

    @RequestMapping("/index")
    public String index() {
        courseList.initial(8);
        selectList.initial(8);
        Course course1 = new Course(0, "软件过程与项目管理", "孙成柱", 5);
        Course course2 = new Course(1, "Python程序设计", "刘景华", 3);
        Course course3 = new Course(2, "计算机网络", "陈永红", 4);
        Course course4 = new Course(3, "数据库系统原理", "喻小光", 3);
        Course course5 = new Course(4, "操作系统", "谢晓东", 1);
        Course course6 = new Course(5, "汇编语言程序设计", "张惠臻", 4);
        Course course7 = new Course(6, "软件建模与分析", "陈苗", 2);
        Course course8 = new Course(7, "数据库应用与开发", "洪欣", 1);
        courseList.add(course1);courseList.add(course2);courseList.add(course3);courseList.add(course4);courseList.add(course5);
        selectList.add(course6);selectList.add(course7);selectList.add(course8);
        return "index";
    }

    @RequestMapping("/login")
    public String login(@RequestParam("name") String name, @RequestParam("password") String password, Model model) {
        if (student1.name.equals(name) && student1.password.equals(password)) {
            model.addAttribute("student", student1);
            model.addAttribute("courses", selectList.list());
            return "main";
        } else {
            return "index";
        }
    }

    @RequestMapping("/toIndex")
    public String toIndex() {
        return "index";
    }


    @RequestMapping("/toMain")
    public String toMain(Model model) {
            model.addAttribute("student", student1);
            model.addAttribute("courses", selectList.list());
            return "main";
    }

    @RequestMapping("/toSelect")
    public String toSelect(Model model) {
        model.addAttribute("student", student1);
        model.addAttribute("courses", courseList.list());

        return "select";

    }

    @RequestMapping("/exitCourse")
    public String exitCourse(int[] exitCourse) {
        for (int i:exitCourse){
            System.out.println(i);
        }
        for (int i : exitCourse){
            courseList.add(selectList.select(i));
            selectList.delete(i);
        }
        return "redirect:/toMain";

    }

    @RequestMapping("/selectCourse")
    public String selectCourse(int[] exitCourse) {
        for (int i : exitCourse){
            selectList.add(courseList.select(i));
            courseList.delete(i);
        }
        return "redirect:/toSelect";

    }





}
